package com.slkj.shilixiaoyuanapp.ui.user.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.CollectEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.notice.NoticeDetailActivity;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.notice.NoticeDetailActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.MySwipeRefreshLayout;
import com.slkj.shilixiaoyuanapp.view.StateLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.PAGE_COUNT;

@ActivityInfo(layout = R.layout.my_collections_activity,title = "我的收藏")
public class MyCollectionsActivity extends BaseActivity{
    @BindView(R.id.recycer)
    RecyclerView recycerType;
    BaseQuickAdapter adapter;
    @BindView(R.id.layout_root)
    StateLayout stateLayout;
    @BindView(R.id.swipeLayout)
    MySwipeRefreshLayout refreshLayout;
    private int page = 1;
    private int count = PAGE_COUNT;
    private int startId = 0;
    List<CollectEntity> collectEntities = new ArrayList<>();
    @Override
    protected void init() {
        recycerType.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        recycerType.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseQuickAdapter<CollectEntity, BaseViewHolder>(R.layout.item_message_list, collectEntities) {
            @Override
            protected void convert(BaseViewHolder helper, final CollectEntity item) {
                helper.setText(R.id.tv_bj,item.getTitle())
                        .setText(R.id.tv_bj_mes,item.getBody())
                        .setText(R.id.tv_time_bj,item.getDate())
                        .itemView.setOnClickListener(v -> {
                    NoticeDetailActivity__JumpCenter.builder(MyCollectionsActivity.this)
                            .setId(item.getId())
                            .setTitle("通知详情")
                            .setType(NoticeDetailActivity.TYPE_COLLECTION)
                            .create().go();
                });
            }
        };

        adapter.setOnLoadMoreListener(this::loadMore,recycerType);
        recycerType.setAdapter(adapter);
        refresh();
        refreshLayout.setOnRefreshListener(this::refresh);
        stateLayout.setOnReLoadListener(() -> refresh());
    }
    /**
     * 加载更多
     */
    private void loadMore() {
        HttpHeper.get().userService()
                .getCollect(startId,page, count)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<CollectEntity>>( ) {
                    @Override
                    public void onCallBackSuccess(List<CollectEntity> data) {
                        setData(false,data);
                        if (!data.isEmpty()) {
                            startId = data.get(data.size() - 1).getId();
                        }
                    }
                });
    }

    /**
     * 刷新
     */
    public void refresh() {
        page = 1;
        startId = 0;
        //这里的作用是防止下拉刷新的时候还可以上拉加载
        adapter.setEnableLoadMore(false);
        HttpHeper.get().userService()
                .getCollect(startId,page, count)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<CollectEntity>>(stateLayout) {
                    @Override
                    public void onCallBackSuccess(List<CollectEntity> data) {
                        if (data.isEmpty()){
                            stateLayout.showEmptyView();
                        }else {
                            startId = data.get(data.size() - 1).getId();
                        }
                        setData(true,data);
                        refreshLayout.setRefreshing(false);
                    }
                    @Override
                    public void onFail(String mes) {
                        super.onFail(mes);
                        refreshLayout.setRefreshing(false);

                    }
                });
    }
    private void setData(boolean isRefresh, List data) {
        final int size = data == null ? 0 : data.size();
        page++;

        if (isRefresh) {
            adapter.setNewData(data);
        } else {
            if (size > 0) {
                adapter.addData(data);
            }
        }
        if (size < count) {
            //第一页如果不够一页就不显示没有更多数据布局
            adapter.loadMoreEnd(isRefresh);
        } else {
            adapter.loadMoreComplete();
        }
    }
}
